package ui.controls;

import java.awt.Color;

import ui.controls.LED;
import ui.control.led.*;
import ui.Assets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cpu.interfaces.*;
import cpu.components.DeviceUnit;
import cpu.datastores.*;

public class LEDBank extends JPanel implements Device {
	protected LED[] bank;
	
	protected int width = 0;
	protected int height = 0;
	protected String name;
	
	private DeviceUnit controller;
	private Register shadow;
	
	public LEDBank( Assets assets, String name, int leds, LEDColors color, LEDSizes size ) {
		setLayout( null );

		this.name = name;
		int start = 48;
		JLabel label = new JLabel( name );
		label.setLocation( 0, 2 );
		label.setSize( start, 48 );
		label.setHorizontalAlignment( SwingConstants.RIGHT);
		this.add( label );
		
		start += 4;
		bank = new LED[leds];
		int width;
		
		this.width = start;
		for( int i = 0; i < bank.length; i++ ) {
			bank[i] = new LED( assets, color, size );
			width = bank[i].getWidth( );
			bank[i].setLocation( start + width * i, 0 );
			this.add( bank[i] );
			
			label = new JLabel( Integer.toString( i ) );
			label.setLocation( start + width * i, width + 6 );
			label.setSize( width, 10 );
			label.setHorizontalAlignment(SwingConstants.CENTER);
			this.add( label );
			
			this.width += width;
		}
		this.height = bank[0].getHeight( ) + 32;
		//setSize( bank[0].getWidth( ) * bank.length, height );
		setSize( this.width, this.height );
		
		switch( leds ) {
		case 1:
			shadow = new Register1( );
			break;
		case 4:
			shadow = new Register4( );
			break;
		case 16:
			shadow = new Register16( );
			break;
		}
		
	}
	
	public void enroll( int id, cpu.components.DeviceUnit controller ) {
		this.controller = controller;
		controller.enroll( id, this );
	}
	
	public void get( ) {

	}
	
	public void put( ) {
		if( shadow.size( ) == 16 ) {
			Register16 shadow16 = (Register16) shadow; 
			shadow16.fromRegister( controller.DBR );
			for( int i = 0; i < 16; i++ ) {
				if( shadow16.bit( i ).value == true )
					toggleOn( i );
				else
					toggleOff( i );
			}
		} else if( shadow.size( ) == 1 ) {
			Register1 shadow1 = (Register1) shadow; 
			shadow1.bit( 0 ).value = controller.DBR.bit( 0 ).value;
			if( shadow1.bit( 0 ).value == true )
				toggleOn( 0 );
			else
				toggleOff( 0 );
		} else if( shadow.size( ) == 4 ) {
			Register4 shadow4 = (Register4) shadow; 
			shadow4.fromJavaInt( controller.DBR.toSignedInteger( ) );
			for( int i = 0; i < 4; i++ ) {
				if( shadow4.bit( i ).value == true )
					toggleOn( i );
				else
					toggleOff( i );
			}
		}
	}
	
	public void status( ) {
		
	}
	
	public int getWidth( ) {
		return width;
	}
	public int getHeight( ) {
		return height;
	}
	public void toggle( int i ) {
		bank[i].toggle( );
	}
	public void toggleOn( int i ) {
		bank[i].toggleOn( );
	}
	public void toggleOff( int i ) {
		bank[i].toggleOff( );
	}
}
